package com.example.asus.drawstudy.activtiy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.asus.drawstudy.R;
import com.example.asus.drawstudy.view.BarChartSportView;
import com.example.asus.drawstudy.view.BarChartView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by liangkun on 2018/8/11.
 * 作用:
 */

public class BarSportActivity extends AppCompatActivity {
    private BarChartSportView barChartView;
    private List<Float> singlelist;
    private RelativeLayout rlSingle;
    private LinearLayout llSingle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar2);
        barChartView = findViewById(R.id.bar_chart2);
        rlSingle = (RelativeLayout) findViewById(R.id.rl_single);
        initData();
        initIntent();
    }

    private void initIntent() {
        startActivity(new Intent(this, HalfActivity.class));
    }

    private void initData() {
        singlelist = new ArrayList<>();
        Random random = new Random();
        while (singlelist.size() < 20) {
            int randomInt = random.nextInt(100);
            singlelist.add((float) randomInt);
        }
        barChartView.setList(singlelist);
//        rlSingle.removeView(llSingle);
        barChartView.setListener(new BarChartSportView.getNumberListener() {
            @Override
            public void getNumber(int number, float x, float y) {
                Log.e("kkk", "number " + number + " x " + x + " y " + y);
                rlSingle.removeView(llSingle);
                llSingle = (LinearLayout) LayoutInflater.from(BarSportActivity.this).inflate(R.layout.layout_pro_expense2, null);
                ImageView tvMoney = llSingle.findViewById(R.id.tv_shouru_pro2);

//                tvMoney.setText(""+(singlelist.get(number)));
                llSingle.measure(0, 0);

                //得到父控件的宽高
                int measuredWidth = llSingle.getMeasuredWidth();
                int measuredHeight = llSingle.getMeasuredHeight();

                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.leftMargin = (int) x - measuredWidth / 2;
                params.topMargin = (int) y - measuredHeight / 2 - 50;
                Log.e("kkk", " params.leftMargin " + params.leftMargin + " params.topMargin " + params.topMargin);

                llSingle.setLayoutParams(params);
                rlSingle.addView(llSingle);
            }
        });
    }
}
